<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Product_Model extends CI_Model
{

	function get_featured_product(){

		//$this->db->where('product_id', $product_id);
		$this->db->limit(12);
		$this->db->where('status',1);
		$this->db->order_by('product_id DESC');

		$query = $this->db->get('product');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();		

	}
	
	function select($product_seo_name){
        $query = $this->db->get_where('product', array('product_seo_name'=>$product_seo_name));
		if($query ->num_rows() >0 ) {
			foreach($query ->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
    }

	function get_list_product($user_id=''){

		$this->db->where('user_id', $user_id);

		$query = $this->db->get('product');

		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function get_store_by_product($product_seo_name){
		return $this->db->query('SELECT * FROM store WHERE user_id in (select user_id from product where product_seo_name = "'.$product_seo_name.'")')-> result();
	}
	
	function get_latest_by_store($store){
		$query = $this->db->query('SELECT * FROM product WHERE user_id in (select user_id from store where toko_id="'.$store.'")order by date_add desc limit 5');

		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	/*function get_category(){

		$query = $this->db->get('product_category');

		//echo $this->db->last_query();

		if ($query->num_rows() > 0) return $query->result();
		return array();
	}*/
	
	function get_category_parent($child){

		return $this->db->query('select * from product_category where category_id in(select parent from product_category where category_id = "'.$child.'" )')->row();
	}
	
	function get_category_child($category_id){
		$this->db->where('category_id',$category_id);
		$query = $this->db->get('product_category');
		return $query->row();
	}
	
	function get_category($category_id){
		$this->db->where('category_id',$category_id);
		$query = $this->db->get('product_category');
		return $query->row();
	}

	//add by dena
	function get_product_by_id($product_id){

		$this->db->where('product_id', $product_id);
		$this->db->where('status',1);
		$query = $this->db->get('product');
		if ($query->num_rows() > 0 ) return $query->row();
		return NULL;	
	} 
	
	function get_option_by_product($product_id){
		$this->db->where('product_id', $product_id);
		$query = $this->db->get('product_option');
		if($query ->num_rows() >0 ) {
			foreach($query ->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
	}
	
	function get_option_by_id($option_id){
		$this->db->where('product_option_id', $option_id);
		$query = $this->db->get('product_option');
		if($query->num_rows() > 0) return $query->row();
		return NULL;
	}
	
	function get_stok_by_id($product_id){
		$this->db->where('product_id', $product_id);
		$query = $this->db->get('product');
		if($query->num_rows() > 0) return $query->row();
		return NULL;
	}
	
}